//
//  MapManager.swift
//  RNForiOS
//
//  Created by 李旭峰 on 7/24/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit
import MapKit

class MapManager: NSObject {

    @objc static func choiceMap() {
        //将User.share.coordinate的两个值改成网吧的经纬度
        let alert = UIAlertController.init(title: "请选择导航应用程序", message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction.init(title: "苹果地图", style: .default, handler: { (action) in
            openIphoneMap(lat: "30.2553245401", lng: "120.1970049222", address: nil)
        }))
        alert.addAction(UIAlertAction.init(title: "百度地图", style: .default, handler: { (action) in
            openBaiduMap(lat: "30.2553245401", lng: "120.1970049222", address: nil)
        }))
        alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: { (action) in
            
        }))
        if let viewController = UIApplication.shared.keyWindow?.rootViewController {
            viewController.present(alert, animated: true, completion: nil)
        }
    }
    
    @objc static func openIphoneMap(lat:String?,lng:String?,address:String?){
        let originLocation = MKMapItem.forCurrentLocation()
        if let tempLat = lat,let tempLng = lng {
            let toLat:CLLocationDegrees = tempLat.decimal.doubleValue
            let toLng:CLLocationDegrees = tempLng.decimal.doubleValue
            let toCoords = CLLocationCoordinate2DMake(toLat, toLng)
            let toPlacemark = MKPlacemark(coordinate: toCoords)
            let toLocation = MKMapItem(placemark: toPlacemark)
            MKMapItem.openMaps(with: [originLocation,toLocation], launchOptions: nil)
        }
    }
    
    @objc static func openBaiduMap(lat:String?,lng:String?,address:String?) {
        var openUrlString = ""
        if UIApplication.shared.canOpenURL(URL(string: "baidumap://")!) {
            openUrlString += "baidumap://map/"
            if let tempLat = lat,let tempLng = lng {
                openUrlString += String(format: "%@?", "geocoder")
                openUrlString += String(format: "location=%@,%@", tempLat,tempLng)
                openUrlString += String(format: "&coord_type=%@", "gcj02")
            }else if let tempAddress = address {
                openUrlString += String(format: "%@?", "geocoder")
                openUrlString += String(format: "address=%@", tempAddress)
            }
            guard let url = URL(string: openUrlString.analyseCnString()) else { return }
            UIApplication.shared.open(url, options: [:]) { (isSuccess) in }
        }else{
            openUrlString += "https://api.map.baidu.com/"
            if let tempLat = lat,let tempLng = lng {
                openUrlString += String(format: "%@?", "geocoder")
                openUrlString += String(format: "location=%@,%@", tempLat,tempLng)
                openUrlString += String(format: "&coord_type=%@", "gcj02")
            }else if let tempAddress = address {
                openUrlString += String(format: "%@?", "geocoder")
                openUrlString += String(format: "address=%@", tempAddress)
            }
            openUrlString += "&output=html"
            guard let url = URL(string: openUrlString.analyseCnString()) else { return }
            UIApplication.shared.open(url, options: [:]) { (isSuccess) in }
        }
    }
}
